#include "ContraPhysics.h"




ContraPhysics::ContraPhysics(ContraData* data)
{
	_data = data;
	evenMax = 15;
	evenCounter = 0;
	isEven = true;
}

void ContraPhysics::stand()
{
	_data->getRect()->Vx = 0;
}

void ContraPhysics::moveRight()
{
	if (_data->notMove) return;
	// set current state to move right and increase x
	_data->getRect()->Vx = MOVE_VX;
}

void ContraPhysics::moveLeft()
{
	if (_data->notMove) return;
	// set current state to move left and descrease x
	_data->getRect()->Vx = -MOVE_VX;
}

void ContraPhysics::update()
{
	updateInNormal();
	updateInWater();
}

void ContraPhysics::updateInNormal()
{
	if (!_data->notMove) {
		_data->getRect()->X += _data->getRect()->Vx;
		_data->getRect()->Y += _data->getRect()->Vy;
	}
	if (_data->checkIsFlying()) {
		_data->jumpingTimeCounter--;
		_data->getRect()->Vy = _data->getRect()->Vy > -6 ? _data->getRect()->Vy - 0.26 : -6;
	} else {
		_data->getRect()->Vy = -6;
	}
	if (_data->delayInteractCounter > 0) {
		_data->delayInteractCounter--;
	} else {
		_data->canInteract = true;
	}
}

void ContraPhysics::updateInWater()
{
	if (!_data->isSwimming) return;
	if (evenCounter > evenMax) {
		evenCounter = 0;
		isEven = !isEven;
	}
	evenCounter++;
	if (evenCounter % 2 == 0)
		_data->getRect()->Y += isEven ? 1.5 : -1.5;
}

void ContraPhysics::jumping()
{
	if (!_data->state == _DATA_CONTRA_STATE_ON_LAND) return;
	if (_data->isSwimming) return;
	if (_data->getBodySprites()->checkState(_STATE_CONTRA_SIT_LEFT) || _data->getBodySprites()->checkState(_STATE_CONTRA_SIT_RIGHT)) {
		_data->canInteract = false;
		_data->delayInteractCounter = _DATA_CONTRA_DELAY_INTERACT;
		//return;
	} 
	if (_data->checkIsFlying()) return;
	_data->jumpingTimeCounter = _DATA_CONTRA_JUMPING_TIME;
	_data->readyToFly = false;
	if (_data->canInteract) {
		_data->getRect()->Vy = 7;
	} else {
		_data->getRect()->Vy = 2;
	}
}

void ContraPhysics::upgradeBullet(int bulletType)
{
	if (bulletType == _ID_BULLET_LEVEL_2) {
		_data->bulletContainer->updateData(_DATA_CONTRA_BULLET_1_DISTANCE_LIVE, _DATA_CONTRA_BULLET_1_DELAY, _DATA_CONTRA_BULLET_2_MAX, _ID_BULLET_LEVEL_2);
	} else if (bulletType == _ID_BULLET_LEVEL_3) {
		_data->bulletContainer->updateData(_DATA_CONTRA_BULLET_1_DISTANCE_LIVE, _DATA_CONTRA_BULLET_3_DELAY, _DATA_CONTRA_BULLET_3_MAX, _ID_BULLET_LEVEL_3);
	} else {
		_data->bulletContainer->updateData(_DATA_CONTRA_BULLET_1_DISTANCE_LIVE, _DATA_CONTRA_BULLET_1_DELAY, _DATA_CONTRA_BULLET_1_MAX, _ID_BULLET_LEVEL_1);
	}
}
